<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!-- 教师操作界面 -->

<!-- 在card-header区域添加 -->
<!-- 主题切换按钮 -->
<button onclick="toggleTheme()" class="theme-toggle" style="
    position: fixed;
    top: 20px;
    right: 20px;
    padding: 10px 15px;
    background: var(--bg-input);
    border: 1px solid var(--border-input);
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
">
    🌙/☀️
</button>
<div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
    <nav class="operation-bar">
        <a href="rDBDashboard.jsp" class="nav-btn">
            <i class="bi bi-speedometer2"></i> 仪表盘
        </a>
        <a href="logout" class="nav-btn">
            <i class="bi bi-box-arrow-right"></i> 退出登录
        </a>
    </nav>
    <h4 class="mb-0">我的成绩</h4>
</div>


    <c:if test="${user.teacher}">
        <h3>成绩管理</h3>
        <div class="upload-box">
            <form action="uploadGradeExcel" method="post" enctype="multipart/form-data">
                <label>上传成绩 Excel（*.xlsx）：</label>
                <div class="custom-file-input-wrapper">
                    <input type="file" name="file" accept=".xlsx" id="fileInput" required>
                    <button type="button" class="file-btn">选择文件</button>
                    <span class="file-name">未选择文件</span>
                </div>
                <button type="submit">批量导入</button>
            </form>
        </div>


        <div class="search-box">
            <form action="gradeManage" method="get" searchKey="">
                <input type="text" name="searchKey" placeholder="输入学号/班级/课程">
                <button type="submit">搜索</button>
            </form>
        </div>

        <!-- 在教师视图的表格中添加评教列 -->
        <table class="grade-table">
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>班级</th>
                <th>课程</th>
                <th>当前成绩</th>
                <th>操作</th>
            </tr>
            <c:forEach items="${relations}" var="sc">
                <tr>
                    <td>${sc.student.studentId}</td>
                    <td>${sc.student.name}</td>
                    <td>${sc.student.className}</td>
                    <td>${sc.course.courseName}</td>
                    <td>
                        <c:choose>
                            <c:when test="${sc.grade != null}">
                                总分：${sc.grade.totalScore}
                            </c:when>
                            <c:otherwise>
                                <span class="no-data">未录入成绩</span>
                            </c:otherwise>
                        </c:choose>
                    </td>
                    <td>
                        <c:choose>
                            <c:when test="${sc.grade != null}">
                                <a href="editGrade?gradeId=${sc.grade.gradeId}">修改</a>
                                <a href="deleteGrade?gradeId=${sc.grade.gradeId}">删除</a>
                            </c:when>
                            <c:otherwise>
                                <a href="addGrade?studentId=${sc.student.studentId}&courseId=${sc.course.courseId}">
                                    添加成绩
                                </a>
                            </c:otherwise>
                        </c:choose>
                    </td>
                </tr>
            </c:forEach>
        </table>
    </c:if>

<!-- 学生视图 -->
<c:if test="${user.student}">
    <h3>我的成绩</h3>
    <table>
        <c:forEach items="${relations}" var="sc">
            <tr>
                <td>课程名:${sc.course.courseName}</td>
                <td>总分:
                    <c:choose>
                        <c:when test="${sc.grade != null}">
                            ${sc.grade.totalScore}
                        </c:when>
                        <c:otherwise>
                            <span class="no-data">未录入成绩</span>
                        </c:otherwise>
                    </c:choose>
                </td>
            </tr>
        </c:forEach>
    </table>
</c:if>
<c:if test="${not empty sessionScope.uploadMessage}">
    <script>
        alert("${sessionScope.uploadMessage}");
    </script>
    <c:remove var="uploadMessage" scope="session"/>
</c:if>


<script>
    document.querySelector('a[href="logout"]').addEventListener('click', function(e) {
        if(!confirm('确定要退出系统吗？')) e.preventDefault();
    });
        // 初始化主题
        function initTheme() {
        const savedTheme = localStorage.getItem('theme') || 'dark';
        const isLight = savedTheme === 'light';
        document.body.classList.toggle('light-mode', isLight);
        document.querySelector('.theme-toggle').textContent = isLight ? '☀️' : '🌙';
    }

        // 切换主题
        function toggleTheme() {
        const body = document.body;
        const isLight = !body.classList.contains('light-mode');
        body.classList.toggle('light-mode');
        localStorage.setItem('theme', isLight ? 'light' : 'dark');
        document.querySelector('.theme-toggle').textContent = isLight ? '☀️' : '🌙';
    }

        // 页面加载时初始化
        window.addEventListener('load', initTheme);
    // 显示选择的文件名
    const fileInput = document.getElementById('fileInput');
    const fileNameSpan = document.querySelector('.file-name');

    fileInput.addEventListener('change', () => {
        if (fileInput.files.length > 0) {
            fileNameSpan.textContent = fileInput.files[0].name;
        } else {
            fileNameSpan.textContent = '未选择文件';
        }
    });

</script>
<link href="./css/rDBGradeManage.css" rel="stylesheet" type="text/css">